package com.thrd.taiexhibition.ui;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * @author: ${唐杰}
 * @date: 2021/4/8
 */
public class LrcView extends RelativeLayout {
    private long duration=1000;
    private AnimatorSet mAnimatorSet;
    private AnimatorSet mAnimatorSet1;
    private TextView mTextView;

    public LrcView(@NonNull Context context) {
        super(context);
    }

    public LrcView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LrcView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    public void addString(String content){
//        showNext();
        if(mTextView!=null){
            outAnimation(mTextView).addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    removeViewAt(0);
                    //    LogUtil.e("LrcView","getChildCount="+LrcView.this.getChildCount());
                }
            });
        }
        RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);
        mTextView = new TextView(getContext());
        mTextView.setText(TextUtils.isEmpty(content)?"":content);
        mTextView.setGravity(Gravity.CENTER);
        mTextView.setLayoutParams(params);
        mTextView.setTextColor(Color.WHITE);
        mTextView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
       // animationView(mTextView,getHeight(),0);
        inAnimation(mTextView);
        addView(mTextView);

    }

    private void showNext(){
            View view1=getChildAt(0);
            if(view1!=null){
                outAnimation(view1).addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        removeViewAt(0);
                    //    LogUtil.e("LrcView","getChildCount="+LrcView.this.getChildCount());
                    }
                });
            }
    }




    //设置入场动画
    private AnimatorSet inAnimation(View view){
        ObjectAnimator animator12=ObjectAnimator.ofFloat(view,"TranslationY",(getHeight()*0.7F),0);
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 0.1f, 1f);
        mAnimatorSet = new AnimatorSet();
        mAnimatorSet.playTogether(animator12,animator);
        mAnimatorSet.setDuration(duration);
        mAnimatorSet.start();
        return mAnimatorSet;
    }

    //设置出动画
    private AnimatorSet outAnimation(View view){
        ObjectAnimator animator12=ObjectAnimator.ofFloat(view,"TranslationY",0,-(getHeight()*0.7F));
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 1f, 0.1f);
        mAnimatorSet1 = new AnimatorSet();
        mAnimatorSet1.playTogether(animator12,animator);
        mAnimatorSet1.setDuration(duration);
        mAnimatorSet1.start();
        return mAnimatorSet1;
    }
}
